#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libusb-1.0/libusb.h>

#define VENDOR_ID 0xVVVV  // 请替换为实际的设备供应商ID
#define PRODUCT_ID 0xPPPP  // 请替换为实际的设备产品ID

#define ENDPOINT_IN 0x81  // 请替换为实际的输入端点地址
#define ENDPOINT_OUT 0x01  // 请替换为实际的输出端点地址

#define TRANSFER_SIZE 512  // 传输大小

int main() {
    libusb_device_handle *devh;
    libusb_context *ctx = NULL;
    int r;

    r = libusb_init(&ctx);
    if (r < 0) {
        fprintf(stderr, "无法初始化 libusb: %s\n", libusb_error_name(r));
        return 1;
    }

    devh = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID);
    if (devh == NULL) {
        fprintf(stderr, "无法打开设备\n");
        libusb_exit(ctx);
        return 1;
    }

    // 从文件读取数据
    FILE *file = fopen("input.txt", "rb");
    if (file == NULL) {
        fprintf(stderr, "无法打开文件\n");
        libusb_close(devh);
        libusb_exit(ctx);
        return 1;
    }

    unsigned char buffer[TRANSFER_SIZE];
    int bytesRead = fread(buffer, 1, TRANSFER_SIZE, file);
    fclose(file);

    // 发送数据
    int transferred;
    r = libusb_bulk_transfer(devh, ENDPOINT_OUT, buffer, bytesRead, &transferred, 0);
    if (r == 0) {
        printf("成功发送 %d 字节的数据\n", transferred);
    } else {
        fprintf(stderr, "发送数据失败: %s\n", libusb_error_name(r));
    }

    // 接收数据
    r = libusb_bulk_transfer(devh, ENDPOINT_IN, buffer, sizeof(buffer), &transferred, 0);
    if (r == 0) {
        printf("成功接收 %d 字节的数据\n", transferred);
        // 处理接收到的数据
    } else {
        fprintf(stderr, "接收数据失败: %s\n", libusb_error_name(r));
    }

    libusb_close(devh);
    libusb_exit(ctx);
    return 0;
}